********************************************************************************
*** Voting for Populism in Europe Replication Files
*** Table 1: Regional Voting (Populist Parties)
*** Table 2: Regional Voting (Centrist Parties)
*** Table A11: Regional Voting, Low Wage Imports (Populist Parties)
*** Table A12: Regional Voting, Low Wage Imports (Centrist Parties)
***
*** Required data files: "imputed_econdata_voteshare_merged.dta"
***                      
*** 
*** Created by: Thomas Cunningham
*** Date: 30 December 2020
******************************************************************
***  Required installations
* ssc install weakivtest
* ssc install avar
* ssc install twostepweakiv 
* ssc install ivreg2
* ssc install moremata
* ssc install ranktest

clear all
version 16.1
cd ..
 	
use "data/imputed/imputed_econdata_voteshare_merged.dta", clear

rename nuts2_main_left_vs mleft
rename nuts2_main_right_vs mright
rename nuts2_left_pop_vs lpop
rename nuts2_right_pop_vs rpop


la var shock_china_ind_L1 "China Shock 1 Lag"
la var shock_lowwage_ind_L1 "Low-Wage Imp Shock 1 Lag"
la var rti_region_L1 "RTI Region 1 Lag"
la var shock_fgn92_L1 "Migrant Shock 1 Lag"
la var shock_robots_mfg_L1 "Robot Shock 1 Lag"
la var shock_fdi_in_ind_L1 "FDI Shock 1 Lag"
la var postcrisis "Post-Crisis"	
la var reg_ict_emp_mfg_L1 "ICT 1 Lag"
la var inst_china_ind_L1 "US-China Instrument 1 Lag"
la var inst_lowwage_ind_L1 "US-Low-Wage Instrument 1 Lag"


local dvs "mleft mright lpop rpop"


local controls0 "shock_fdi_in_ind_L1  shock_robots_mfg_L1"
local controls1 "shock_fdi_in_ind_L1  shock_robots_mfg_L1 rti_region_L1 shock_fgn92_L1"

* Regression Loop 

foreach v of  varlist `dvs' {
	di "`v'"
	forval u = 0/1{ 
* Non-Interaction			
	mi est, post: reg `v' shock_china_ind_L1 i.postcrisis  `controls`u''  i.cwdyad, robust cluster(nuts_encoded)
	est store cn_`v'_c`u'_1
	di "`v'"
	
	sleep 5000
* Interact China Shock x Vote Share
	mi est, post: reg `v' c.shock_china_ind_L1##i.postcrisis   `controls`u''  i.cwdyad, robust cluster(nuts_encoded)
	est store pccn_`v'_c`u'_1
	di "`v'"
	sleep 5000
	
* Non-Interaction Low-Wage
	mi est, post: reg `v' shock_lowwage_ind_L1 i.postcrisis  `controls`u''  i.cwdyad, robust cluster(nuts_encoded)
	est store lw_`v'_c`u'_1
	di "`v'"
	sleep 5000

* Interaction Low-Wage Shock X Post Crisis
	mi est, post: reg `v' c.shock_lowwage_ind_L1##i.postcrisis   `controls`u''  i.cwdyad, robust cluster(nuts_encoded)
	est store pclw_`v'_c`u'_1
	di "`v'"
	sleep 5000
	
* 2SLS China Estimation Model (Non-interaction)
	mi est, cmdok post: ivreg2 `v' i.postcrisis  `controls`u''  (shock_china_ind_L1 = inst_china_ind_L1) i.cwdyad, partial(i.cwdyad) robust cluster(nuts_encoded) first, 
	est store cn_`v'_c`u'_1_inst
	di "`v'"
	sleep 5000
	
* 2SLS Interact China Shock x Vote Share
	mi est, cmdok post: ivreg2 `v' `controls`u'' (c.shock_china_ind_L1##i.postcrisis = c.inst_china_ind_L1##i.postcrisis) i.cwdyad , partial(i.cwdyad) robust cluster(nuts_encoded) first, 
	est store pccn_`v'_c`u'_1_inst
	di "`v'"
	sleep 5000
	
* 2SLS Lowwage Estimation (Non-interaction)
	mi est, cmdok post: ivreg2 `v' i.postcrisis  `controls`u''  (shock_lowwage_ind_L1 = inst_lowwage_ind_L1) i.cwdyad , partial(i.cwdyad) robust cluster(nuts_encoded), 
	est store lw_`v'_c`u'_1_inst
	di "`v'"
	
* 2SLS Lowwage Estimation (Interaction)
	mi est, cmdok post: ivreg2 `v' `controls`u'' (c.shock_lowwage_ind_L1##i.postcrisis = c.inst_lowwage_ind_L1##i.postcrisis) i.cwdyad, partial(i.cwdyad) robust cluster(nuts_encoded), 
	est store pclw_`v'_c`u'_1_inst
	di "`v'"
		}
	

* Tables 1 and 2 
esttab cn_`v'_c0_1  cn_`v'_c0_1_inst cn_`v'_c1_1 cn_`v'_c1_1_inst pccn_`v'_c0_1 pccn_`v'_c0_1_inst pccn_`v'_c1_1  pccn_`v'_c1_1_inst using "output/regional_voting_`v'.tex", replace /*
*/  cells(b(star fmt(%9.3f)) se(par)) stats(N, fmt(%9.0g %9.3f)) unstack starlevels(* .1 ** .05 *** .01)/*
	*/ label legend drop(*.cwdyad) nobaselevels  order(shock_china_ind_L1 1.postcrisis shock_fdi_in_ind_L1  shock_robots_mfg_L1 rti_region_L1  shock_fgn92_L1) nodepvars nomtitles  ///
	title("Regional Voting (1990-2018) \\ (Instrumental Variable, All original Models)} \begin{threeparttable") nonumber  nonotes compress ///
postfoot("\hline\hline \end{tabular} \begin{tablenotes} \footnotesize \item Notes: * p$<$.1, ** p$<$.05, *** p$<$.01. OLS estimates with country-year (i.e., election) fixed effects and robust standard errors clustered over 160 European regions (NUTS-1/2) in parentheses. The dependent variable fis vote share as a percentage by party family. Globalization shocks are three-year differences in total imports, inward FDI flows in manufacturing, and the stock of robots in manufacturing, all weighted by labor shares in industry, regional-level exposure to routinization and off-shoring, and the portion of migrants over the region's total population. \end{tablenotes} \end{threeparttable} \end{table}") 

* Tables A11 and A12 
esttab lw_`v'_c0_1  lw_`v'_c0_1_inst lw_`v'_c1_1 lw_`v'_c1_1_inst pclw_`v'_c0_1 pclw_`v'_c0_1_inst pclw_`v'_c1_1  pclw_`v'_c1_1_inst using "output/appendix/regional_voting_lowwage_`v'.tex", replace /*
*/  cells(b(star fmt(%9.3f)) se(par)) stats(N, fmt(%9.0g %9.3f)) unstack starlevels(* .1 ** .05 *** .01)/*
	*/ label legend drop(*.cwdyad) nobaselevels  order(shock_lowwage_ind_L1 1.postcrisis shock_fdi_in_ind_L1  shock_robots_mfg_L1 rti_region_L1  shock_fgn92_L1) nodepvars nomtitles  ///
	title("Regional Voting (1990-2018) \\ (Instrumental Variable, All original Models)} \begin{threeparttable") nonumber  nonotes compress ///
postfoot("\hline\hline \end{tabular} \begin{tablenotes} \footnotesize \item Notes: * p$<$.1, ** p$<$.05, *** p$<$.01. OLS estimates with country-year (i.e., election) fixed effects and robust standard errors clustered over 160 European regions (NUTS-1/2) in parentheses. The dependent variable fis vote share as a percentage by party family. Globalization shocks are three-year differences in total imports, inward FDI flows in manufacturing, and the stock of robots in manufacturing, all weighted by labor shares in industry, regional-level exposure to routinization and off-shoring, and the portion of migrants over the region's total population. \end{tablenotes} \end{threeparttable} \end{table}")



estimates clear
}






local dvs "mleft mright lpop rpop"

local controls0 "shock_fdi_in_ind_L1  shock_robots_mfg_L1"
local controls1 "shock_fdi_in_ind_L1  shock_robots_mfg_L1 rti_region_L1 shock_fgn92_L1"
local controls2 "shock_fdi_in_ind_L1  shock_robots_mfg_L1"
local controls3 "shock_fdi_in_ind_L1  shock_robots_mfg_L1 rti_region_L1 shock_fgn92_L1"

****************************************************************************
*** Kleibergen-Paap rk Wald F statistic estimate
****************************************************************************


***************************************
*** China-US Instrument 
***************************************

foreach dvar of varlist `dvs'{
    forval u = 0/1{
		preserve
		
		* Loop over imputed datasets
		forvalues j = 0 1 to 30{ 
			qui   ivreg2 `dvar' i.postcrisis `controls`u'' (shock_china_ind_L1 = inst_china_ind_L1) i.cwdyad if _mi_m == `j', partial(i.cwdyad) robust cluster(nuts_encoded) first
			gen F_stat`j' = e(rkf)
		}
		
		keep F_stat*
		egen sum = rowtotal(F_stat*)
		gen meanF_`dvar'`u' = sum/31
		duplicates drop 
		
		save "data/`dvar'_F_model`u'.dta", replace
		
		restore
	}
	estimates clear
}
foreach dvar of varlist `dvs'{
	forval u = 2/3{
		preserve
		forvalues j = 0 1 to 30{ 
			qui   ivreg2 `dvar' `controls`u'' (c.shock_china_ind_L##i.postcrisis = c.inst_china_ind_L##i.postcrisis) i.cwdyad if _mi_m == `j', partial(i.cwdyad) robust cluster(nuts_encoded) first
			
			gen F_stat`j' = e(rkf)
		}
		
		keep F_stat*
		egen sum = rowtotal(F_stat*)
		gen meanF_`dvar'`u' = sum/31
		duplicates drop 
		
		save "data/`dvar'_F_model`u'.dta", replace
		restore
	}
	estimates clear
}
preserve
forval u = 0/3{
    * Merges into a combined Model file
	* Data should be equal, regardless of DV, so the full set is kept to 
	* show that this is indeed true
	use "data/rpop_F_model`u'.dta", clear
	append using "data/lpop_F_model`u'.dta"
	append using "data/mleft_F_model`u'.dta"
	append using "data/mright_F_model`u'.dta"
	
	keep meanF*
	save "output/F_model`u'.dta", replace
	
	erase "data/rpop_F_model`u'.dta"
	erase "data/lpop_F_model`u'.dta"
	erase "data/mleft_F_model`u'.dta"
	erase "data/mright_F_model`u'.dta"
}
restore

estimates clear

*************************************
*** Lowwage-US Instrument
*************************************
local dvs "mleft mright lpop rpop"


local controls0 "shock_fdi_in_ind_L1  shock_robots_mfg_L1"
local controls1 "shock_fdi_in_ind_L1  shock_robots_mfg_L1 rti_region_L1 shock_fgn92_L1"
local controls2 "shock_fdi_in_ind_L1  shock_robots_mfg_L1"
local controls3 "shock_fdi_in_ind_L1  shock_robots_mfg_L1 rti_region_L1 shock_fgn92_L1"
foreach dvar of varlist `dvs'{
    forval u = 0/1{
		preserve
		forvalues j = 0 1 to 30{
			qui   ivreg2 `dvar' i.postcrisis `controls`u'' (shock_lowwage_ind_L1 = inst_lowwage_ind_L1) i.cwdyad if _mi_m == `j', partial(i.cwdyad) robust cluster(nuts_encoded) first
			gen F_stat`j' = e(rkf)
		}
		keep F_stat*
		egen sum = rowtotal(F_stat*) 
		gen meanF_`dvar'`u' = sum/31
		
		duplicates drop 
		
		save "data/`dvar'_F_lwmodel`u'.dta", replace
		
		restore
	}
	estimates clear
}
foreach dvar of varlist `dvs'{
    forval u = 2/3{
		preserve
		forvalues j = 0 1 to 30{ 
			qui ivreg2 `dvar' `controls`u'' (c.shock_lowwage_ind_L##i.postcrisis = c.inst_lowwage_ind_L##i.postcrisis) i.cwdyad if _mi_m == `j', partial(i.cwdyad) robust cluster(nuts_encoded) first
			gen F_stat`j' = e(rkf)
		}
		keep F_stat*
		egen sum = rowtotal(F_stat*)
		gen meanF_`dvar'`u' = sum/31
		duplicates drop 
		
		save "data/`dvar'_F_lwmodel`u'.dta", replace
		restore
	}
	estimates clear
}

preserve
forval u = 0/3{
	use "data/rpop_F_lwmodel`u'.dta", clear
	append using "data/lpop_F_lwmodel`u'.dta"
	append using "data/mleft_F_lwmodel`u'.dta"
	append using "data/mright_F_lwmodel`u'.dta"
	keep meanF*
	save "output/appendix/F_lwmodel`u'.dta", replace
	erase "data/rpop_F_lwmodel`u'.dta"
	erase "data/lpop_F_lwmodel`u'.dta"
	erase "data/mleft_F_lwmodel`u'.dta"
	erase "data/mright_F_lwmodel`u'.dta"
}
restore

estimates clear

********************************************************************************
******* Anderson Rubin Confidence Intervals
********************************************************************************

***************************
*** US-China Instrument
***************************


local dvs "mleft mright lpop rpop"

local controls0 "shock_fdi_in_ind_L1  shock_robots_mfg_L1"
local controls1 "shock_fdi_in_ind_L1  shock_robots_mfg_L1 rti_region_L1 shock_fgn92_L1"


foreach dvar of varlist `dvs'{
    forval u = 0/1{
	    preserve
		forvalues j = 0/30{
		    twostepweakiv 2sls `dvar' i.postcrisis `controls`u'' i.cwdyad (shock_china_ind_L1 = inst_china_ind_L1) if _mi_m == `j', partial(i.cwdyad) robust cluster(nuts_encoded)
			
			* twostepweakiv generates the AR confidence intervals as string data with brackets
			* This creates separate AR lower bound and upper bound vars for the AR conf. int.
			gen arlow_`j' = substr(e(ar_cset), 2, 8)
			gen arhigh_`j' = substr(e(ar_cset), 12, 7)
			destring arlow_`j', replace
			destring arhigh_`j', replace
		}
		keep ar*
		egen sum_arlow = rowtotal(arlow*)
		egen sum_arhigh = rowtotal(arhigh*)
		gen mean_arlow_`dvar'`u' = sum_arlow/30
		gen mean_arhigh_`dvar'`u' = sum_arhigh/30
		duplicates drop 
		
		save "data/`dvar'_ARCI_model`u'.dta", replace
		
		restore
	}
}

preserve
forval u = 0/1{
    use "data/rpop_ARCI_model`u'.dta", clear
	append using "data/lpop_ARCI_model`u'.dta"
	append using "data/mleft_ARCI_model`u'.dta"
	append using "data/mright_ARCI_model`u'.dta"
	keep mean_ar*
	save "output/ARCI_model`u'.dta", replace
	erase "data/rpop_ARCI_model`u'.dta"
	erase "data/lpop_ARCI_model`u'.dta"
	erase "data/mleft_ARCI_model`u'.dta"
	erase "data/mright_ARCI_model`u'.dta"
}
restore

estimates clear


***************************
*** Lowwage US Instrument 
***************************
local dvs "mleft mright lpop rpop"

local controls0 "shock_fdi_in_ind_L1  shock_robots_mfg_L1"
local controls1 "shock_fdi_in_ind_L1  shock_robots_mfg_L1 rti_region_L1 shock_fgn92_L1"

foreach dvar of varlist `dvs'{
    forval u = 0/1{
	    preserve
		forvalues j = 1/30{
		    twostepweakiv 2sls `dvar' i.postcrisis `controls`u'' i.cwdyad (shock_lowwage_ind_L1 = inst_lowwage_ind_L1) if _mi_m == `j', partial(i.cwdyad) robust cluster(nuts_encoded)

			gen arlow_`j' = substr(e(ar_cset), 2, 8)
			gen arhigh_`j' = substr(e(ar_cset), 12, 7)
			destring arlow_`j', replace
			destring arhigh_`j', replace
		}	
		keep ar*	
		egen sum_arlow = rowtotal(arlow*)
		egen sum_arhigh = rowtotal(arhigh*)
		gen mean_arlow_`dvar'`u' = sum_arlow/30
		gen mean_arhigh_`dvar'`u' = sum_arhigh/30
		duplicates drop 
		save "data/`dvar'_ARCI_lwmodel`u'.dta", replace
		restore
	}
}

preserve

forval u = 0/1{
    use "data/rpop_ARCI_lwmodel`u'.dta", clear
	append using "data/lpop_ARCI_lwmodel`u'.dta"
	append using "data/mleft_ARCI_lwmodel`u'.dta"
	append using "data/mright_ARCI_lwmodel`u'.dta"
	keep mean_ar*
	save "output/appendix/ARCI_lwmodel`u'.dta", replace
	erase "data/rpop_ARCI_lwmodel`u'.dta"
	erase "data/lpop_ARCI_lwmodel`u'.dta"
	erase "data/mleft_ARCI_lwmodel`u'.dta"
	erase "data/mright_ARCI_lwmodel`u'.dta"
}
restore


**********************************************************************************
***First stage regression results
**********************************************************************************

**************************
*** US China Instrument 
**************************

local controls0 "shock_fdi_in_ind_L1  shock_robots_mfg_L1"
local controls1 "shock_fdi_in_ind_L1  shock_robots_mfg_L1 rti_region_L1 shock_fgn92_L1"
local controls2 "shock_fdi_in_ind_L1  shock_robots_mfg_L1"
local controls3 "shock_fdi_in_ind_L1  shock_robots_mfg_L1 rti_region_L1 shock_fgn92_L1"

* Looping over controls/models 
* The DV does not have an impact on the first stage results, so this is uniform
* across DVs

forval u = 0/1{
    mi est, cmdok post: ivreg2 shock_china_ind_L1 inst_china_ind_L1 i.postcrisis `controls`u'' i.cwdyad, partial(i.cwdyad) robust cluster(nuts_encoded) first,
	if `u' == 0{
	    outreg2 using "output/maintext_tables_firststage_models1_2.tex", replace ctitle(Model `u') label bracket bdec(3) 
	}
	else{
	    outreg2 using "output/maintext_tables_firststage_models1_2.tex", append ctitle(Model `u') label bracket bdec(3) 
	}
}

forval u = 2/3{
    mi est, cmdok post: ivreg2 shock_china_ind_L1 c.inst_china_ind_L1##i.postcrisis `controls`u'' i.cwdyad, partial(i.cwdyad) robust cluster(nuts_encoded) first,
	if `u' == 2{
	    outreg2 using "output/maintext_tables_firststage_models3_4.tex", replace ctitle(Model `u') label bracket bdec(3) 
	}
	else{
	    outreg2 using "output/maintext_tables_firststage_models3_4.tex", append ctitle(Model `u') label bracket bdec(3) 	
	}
}


*************************
*** Lowwage US Instrument
*************************

local controls0 "shock_fdi_in_ind_L1  shock_robots_mfg_L1"
local controls1 "shock_fdi_in_ind_L1  shock_robots_mfg_L1 rti_region_L1 shock_fgn92_L1"
local controls2 "shock_fdi_in_ind_L1  shock_robots_mfg_L1"
local controls3 "shock_fdi_in_ind_L1  shock_robots_mfg_L1 rti_region_L1 shock_fgn92_L1"

forval u = 0/1{
    mi est, cmdok post: ivreg2 shock_lowwage_ind_L1 inst_lowwage_ind_L1 i.postcrisis `controls`u'' i.cwdyad, partial(i.cwdyad) robust cluster(nuts_encoded) first,
	if `u' == 0{
	    outreg2 using "output/appendix/lowwage_tables_firststage_models1_2.tex", replace ctitle(Model `u') label bracket bdec(3) 
	}
	else{
	    outreg2 using "output/appendix/lowwage_tables_firststage_models1_2.tex", append ctitle(Model `u') label bracket bdec(3) 
	}
}

forval u = 2/3{
    mi est, cmdok post: ivreg2 shock_lowwage_ind_L1 c.inst_lowwage_ind_L1##i.postcrisis `controls`u'' i.cwdyad, partial(i.cwdyad) robust cluster(nuts_encoded) first,
	if `u' == 2{
	    outreg2 using "output/appendix/lowwage_tables_firststage_models2_3.tex", replace ctitle(Model `u') label bracket bdec(3) 
	}
	else{
	    outreg2 using "output/appendix/lowwage_tables_firststage_models2_3.tex", append ctitle(Model `u') label bracket bdec(3) 
	}
}